package com.chunxuan.langquan.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CourseSearch;
import com.chunxuan.langquan.dao.http.APIRetrofit2;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 搜索页面
public class SearchCourseActivity extends Base2Activity {
    private ImageView iv_back_vv;
    private TextView tv_search;
    private AppCompatEditText et_word;// 输入的搜索关键字
    private RecyclerView rcv_list;// 课程列表

    private String word;// 记录搜索的关键字

    private List<CourseSearch> courseSearchList = new ArrayList<>();
    private SearchListAdapter searchListAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_search_course;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_back_vv = findViewById(R.id.iv_back_vv);
        tv_search = findViewById(R.id.tv_search);
        et_word = findViewById(R.id.et_word);
        rcv_list = findViewById(R.id.rcv_list);

        setOnClickListener(iv_back_vv, tv_search);

        GridLayoutManager manager = new GridLayoutManager(this, 2);
        rcv_list.setLayoutManager(manager);
        searchListAdapter = new SearchListAdapter(this);
        rcv_list.setAdapter(searchListAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_search) {// 点击搜索
            word = et_word.getText().toString().trim();
            if (TextUtils.isEmpty(word)) {
                ToastUtils.showShort("请输入课程名称");
                return;
            }

            hideProgress();
            reqSerach();
            showProgress();
        }
        if (v == iv_back_vv) {// 返回
            onBackPressed();
        }
    }

    private Disposable reqSerach() {
        return APIRetrofit2.getDefault().reqSearchCourseList(Global.HEADER, word)
                .compose(RxUtil.subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<CourseSearch>>>() {
                    @Override
                    public void accept(BaseResult2<List<CourseSearch>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {
                            courseSearchList = listResult2.getData();
                            searchListAdapter.setDataList(courseSearchList);
                        } else {
                            ToastUtils.showShort(listResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        hideProgress();
                    }
                });
    }

    private class SearchListAdapter extends ListBaseAdapter<CourseSearch> {

        public SearchListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_subject_more_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            TextView tv_course_title = holder.getView(R.id.tv_course_title);
            TextView tv_subject_summary = holder.getView(R.id.tv_subject_summary);

            CourseSearch courseSearch = mDataList.get(position);
            tv_course_title.setText(courseSearch.getTitle());
            tv_subject_summary.setText(courseSearch.getSummary());

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SearchCourseActivity.this, CourseDetailActivity.class);
                    intent.putExtra("grade_category_id", courseSearch.getGrade_category_id());
                    intent.putExtra("subject_category_id", courseSearch.getSubject_category_id());
                    intent.putExtra("class_category_id", courseSearch.getClass_category_id());
                    intent.putExtra("course_id", courseSearch.getId());
                    startActivity(intent);
                }
            });
        }
    }
}